﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using QXBook.Utils;
using System.Web;
using QXBook.Service;
using QXBook.Model;
using System.Configuration;

namespace QXBook.Handlers.AjaxHandlers
{
    public class AddFavorite : AjaxHandler, IRequiresSessionState
    {
        public override string GetBusinessData(HttpContext context)
        {
            int productID = CommonUtils.ToInt(context.Request["id"]);

            object username = SessionState.Get(ConfigurationManager.AppSettings["UserSessionName"]);

            UsersInfo user = null;
            if (username == null)
            {
                return "请先登录.";
            }
            user = ServiceFactory.CreateInstance<UsersService>().GetUsersInfo(username.ToString());

            bool result = ServiceFactory.CreateInstance<FavoritesService>().CheckFavorite(productID, user.UserID);

            if (result)
            {
                return "该商品您已经收藏了.";
            }
            FavoritesInfo fav = new FavoritesInfo();
            fav.ProductID = productID;
            fav.UserID = user.UserID;
            return ServiceFactory.CreateInstance<FavoritesService>().AddFavorites(fav);
        }
    }
}
